package com.bsit.obu.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 判断是否有空对象
 */
public class BeanUtil {

	 public static boolean checkFieldValueNull(Object bean) {
	 boolean result = true;
	 if (bean == null) {
	 return true;
	}
	 Class<?> cls = bean.getClass();
	 Method[] methods = cls.getDeclaredMethods();
	 Field[] fields = cls.getDeclaredFields();
	 for (Field field : fields) {
	 try {
	 String fieldGetName = parGetName(field.getName());
	 if (!checkGetMet(methods, fieldGetName)) {
	continue;
	}
	 Method fieldGetMet = cls.getMethod(fieldGetName, new Class[]{});
	 Object fieldVal = fieldGetMet.invoke(bean, new Object[]{});
	 if (fieldVal != null) {
		 if ("".equals(fieldVal)) {
			 return true;
		 } else {
			 result = false;
		}
	} else {
		return true;
	}
	 } catch (Exception e) {
	continue;
	}
	}
	 return result;
	}


	/**
	 * ƴ��ĳ���Ե� get����
	*
	 * @param fieldName
	 * @return String
	*/
	 public static String parGetName(String fieldName) {
	 if (null == fieldName ||"".equals(fieldName)) {
	 return null;
	}
	 int startIndex = 0;
	 if (fieldName.charAt(0) == '_')
	 startIndex = 1;
	 return"get"
	 + fieldName.substring(startIndex, startIndex + 1).toUpperCase()
	 + fieldName.substring(startIndex + 1);
	}

	/**
	 * �ж��Ƿ����ĳ���Ե� get����
	*
	 * @param methods
	 * @param fieldGetMet
	 * @return boolean
	*/
	 public static boolean checkGetMet(Method[] methods, String fieldGetMet) {
	 for (Method met : methods) {
	 if (fieldGetMet.equals(met.getName())) {
	 return true;
	}
	}
	 return false;
	}

}
